import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isNil from "lodash/isNil";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class;

/**
 * TODO: 可优化， 详情和列表都使用到了该组件 可以自己直接 connect 连接 redux
 * SystemNoticePublishModal
 * @date 2019-06-12
 */
import React, { Component } from 'react';
import { Button, Card, Col, Drawer, Popconfirm, Row, Spin, Table } from 'antd';
import { Bind, Debounce } from 'lodash-decorators';
import notification from 'utils/notification';
import intl from 'utils/intl';
import { DEBOUNCE_TIME, DETAIL_CARD_TABLE_THIRD_CLASSNAME, EDIT_FORM_ROW_LAYOUT, FORM_COL_2_LAYOUT } from 'utils/constants';
import { dateTimeRender, operatorRender } from 'utils/renderer';
import SystemNoticePublishAddDrawer from "./SystemNoticePublishAddDrawer";

function historyRowKeyFunc(record, index) {
  return "".concat(index, "-").concat(record);
}

var SystemNoticePublishDrawer = (_dec = Bind(), _dec2 = Bind(), _dec3 = Debounce(DEBOUNCE_TIME), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), (_class = /*#__PURE__*/function (_Component) {
  _inherits(SystemNoticePublishDrawer, _Component);

  var _super = _createSuper(SystemNoticePublishDrawer);

  function SystemNoticePublishDrawer(props) {
    var _this;

    _classCallCheck(this, SystemNoticePublishDrawer);

    _this = _super.call(this, props);
    _this.state = {
      addDrawerVisible: false,
      cacheHistoryPagination: {},
      cacheReceivePagination: {}
    };
    return _this;
  }

  _createClass(SystemNoticePublishDrawer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.handleHistorySearch();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.handleHistoryTableRowChange.cancel();
    }
    /**
     * // 会重置 选中/分页 右侧的状态
     * 查询历史记录
     * @param pagination
     */

  }, {
    key: "handleHistorySearch",
    value: function handleHistorySearch() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          querySystemHistory = _this$props.querySystemHistory,
          record = _this$props.record;
      this.setState({
        cacheHistoryPagination: pagination
      });
      return querySystemHistory({
        noticeId: record.noticeId,
        query: pagination
      });
    }
    /**
     * 查询接受信息
     * @param {object} query 分页信息 或者 选中的历史信息
     * @param {string} type - page history reload
     * page - 传递 {page, sort}
     * history - 传递 {systemNoticeHistorySelectedRows, systemNoticeHistorySelectedRowKeys}
     * reload - 传递 state 中的 cacheReceivePagination
     */

  }, {
    key: "handleReceiveSearch",
    value: function handleReceiveSearch() {
      var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var type = arguments.length > 1 ? arguments[1] : undefined;
      var _this$props2 = this.props,
          queryReceiver = _this$props2.queryReceiver,
          record = _this$props2.record;

      switch (type) {
        case 'page':
          this.setState({
            cacheReceivePagination: query
          });
          break;

        case 'history':
          break;

        case 'reload':
          break;

        default:
          break;
      }

      return queryReceiver({
        noticeId: record.noticeId,
        query: query
      });
    } // Drawer

    /**
     * 关闭 Drawer
     */

  }, {
    key: "handleClose",
    value: function handleClose() {
      var onCancel = this.props.onCancel;
      onCancel();
    } // Left Table

  }, {
    key: "handleHistoryTableChange",
    value: function handleHistoryTableChange(page, filter, sort) {
      this.handleHistorySearch({
        page: page,
        sort: sort
      });
    }
  }, {
    key: "handleHistoryTableRowChange",
    value: function handleHistoryTableRowChange(_, selectedRows) {
      var selectedRowKeys = selectedRows.map(function (r) {
        return r.publishedId;
      });
      this.handleReceiveSearch({
        systemNoticeHistorySelectedRows: selectedRows,
        systemNoticeHistorySelectedRowKeys: selectedRowKeys
      }, 'history');
    }
  }, {
    key: "getHistoryColumns",
    value: function getHistoryColumns() {
      return [{
        dataIndex: 'publishedStatusMeaning',
        title: intl.get('cmsg.notice.model.publish.publishedStatus').d('发布状态'),
        width: 100
      }, {
        dataIndex: 'publishedDate',
        title: intl.get('cmsg.notice.model.publish.publishedDate').d('发布时间'),
        render: dateTimeRender,
        width: 160
      }, {
        dataIndex: 'publisherName',
        title: intl.get('cmsg.notice.model.publish.publisherName').d('发布人')
      }];
    } // Right Table

  }, {
    key: "handleReceiveTableChange",
    value: function handleReceiveTableChange(page, filter, sort) {
      this.handleReceiveSearch({
        page: page,
        sort: sort
      }, 'page');
    }
  }, {
    key: "handleRemoveDraftReceive",
    value: function handleRemoveDraftReceive(record) {
      var _this2 = this;

      var removeDraftReceive = this.props.removeDraftReceive;
      removeDraftReceive({
        record: record
      }).then(function (res) {
        if (res) {
          var _this2$state$cacheRec = _this2.state.cacheReceivePagination,
              cacheReceivePagination = _this2$state$cacheRec === void 0 ? {} : _this2$state$cacheRec;

          _this2.handleReceiveSearch(cacheReceivePagination, 'reload');
        }
      });
    }
  }, {
    key: "getReceiveColumns",
    value: function getReceiveColumns() {
      var _this3 = this;

      return [{
        dataIndex: 'receiverTypeMeaning',
        title: intl.get('cmsg.common.view.type').d('类型'),
        width: 100
      }, {
        dataIndex: 'receiverSourceName',
        title: intl.get('cmsg.notice.model.receive.receiverSource').d('接收方名称')
      }, {
        key: 'action',
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 80,
        render: function render(_, record) {
          var operators = [];

          if (!_isNil(record.receiverId)) {
            operators.push({
              key: 'delete',
              ele: /*#__PURE__*/React.createElement(Popconfirm, {
                onConfirm: function onConfirm() {
                  _this3.handleRemoveDraftReceive(record);
                },
                title: intl.get('cpaas.common.message.confirm.delete').d('是否删除此条记录？')
              }, /*#__PURE__*/React.createElement("a", null, intl.get('cpaas.common.button.delete').d('删除'))),
              len: 2,
              title: intl.get('cpaas.common.button.delete').d('删除')
            });
          }

          return operatorRender(operators);
        }
      }];
    } // Right Btn

  }, {
    key: "handleAddBtnClick",
    value: function handleAddBtnClick() {
      // 打开 新增接收配置的 模态框
      this.setState({
        addDrawerVisible: true
      });
    }
  }, {
    key: "handlePublishBtnClick",
    value: function handlePublishBtnClick() {
      var _this4 = this;

      var _this$props3 = this.props,
          record = _this$props3.record,
          publishSystemNotice = _this$props3.publishSystemNotice,
          _this$props3$systemNo = _this$props3.systemNoticeHistorySelectedRows,
          systemNoticeHistorySelectedRows = _this$props3$systemNo === void 0 ? [] : _this$props3$systemNo;
      publishSystemNotice({
        noticeId: record.noticeId,
        records: systemNoticeHistorySelectedRows.map(function (r) {
          return r.publishedId;
        })
      }).then(function (res) {
        if (res) {
          var _this4$state$cacheHis = _this4.state.cacheHistoryPagination,
              cacheHistoryPagination = _this4$state$cacheHis === void 0 ? {} : _this4$state$cacheHis;

          _this4.handleHistorySearch(cacheHistoryPagination);
        }
      });
    } // SystemNoticePublishAddDrawer

  }, {
    key: "handleAddDrawerClose",
    value: function handleAddDrawerClose() {
      this.setState({
        addDrawerVisible: false
      });
    }
  }, {
    key: "handleAddReceiver",
    value: function handleAddReceiver(payload) {
      var _this5 = this;

      var createReceiver = this.props.createReceiver;
      createReceiver(payload).then(function (res) {
        if (res) {
          notification.success();
          var _this5$state$cacheHis = _this5.state.cacheHistoryPagination,
              cacheHistoryPagination = _this5$state$cacheHis === void 0 ? {} : _this5$state$cacheHis;

          _this5.handleHistorySearch(cacheHistoryPagination);

          _this5.handleAddDrawerClose();
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
          visible = _this$props4.visible,
          _this$props4$systemNo = _this$props4.systemNoticeHistoryDataSource,
          systemNoticeHistoryDataSource = _this$props4$systemNo === void 0 ? [] : _this$props4$systemNo,
          _this$props4$systemNo2 = _this$props4.systemNoticeHistoryPagination,
          systemNoticeHistoryPagination = _this$props4$systemNo2 === void 0 ? false : _this$props4$systemNo2,
          _this$props4$systemNo3 = _this$props4.systemNoticeHistorySelectedRows,
          systemNoticeHistorySelectedRows = _this$props4$systemNo3 === void 0 ? [] : _this$props4$systemNo3,
          _this$props4$systemNo4 = _this$props4.systemNoticeHistorySelectedRowKeys,
          systemNoticeHistorySelectedRowKeys = _this$props4$systemNo4 === void 0 ? [] : _this$props4$systemNo4,
          _this$props4$systemNo5 = _this$props4.systemNoticeReceiveDataSource,
          systemNoticeReceiveDataSource = _this$props4$systemNo5 === void 0 ? [] : _this$props4$systemNo5,
          _this$props4$systemNo6 = _this$props4.systemNoticeReceivePagination,
          systemNoticeReceivePagination = _this$props4$systemNo6 === void 0 ? false : _this$props4$systemNo6,
          receiverRecordType = _this$props4.receiverRecordType,
          record = _this$props4.record,
          organizationId = _this$props4.organizationId,
          isTenantRole = _this$props4.isTenantRole,
          _this$props4$querySys = _this$props4.querySystemHistoryLoading,
          querySystemHistoryLoading = _this$props4$querySys === void 0 ? false : _this$props4$querySys,
          _this$props4$queryRec = _this$props4.queryReceiverLoading,
          queryReceiverLoading = _this$props4$queryRec === void 0 ? false : _this$props4$queryRec,
          createReceiverLoading = _this$props4.createReceiverLoading,
          _this$props4$publishS = _this$props4.publishSystemNoticeLoading,
          publishSystemNoticeLoading = _this$props4$publishS === void 0 ? false : _this$props4$publishS,
          _this$props4$removeDr = _this$props4.removeDraftReceiveLoading,
          removeDraftReceiveLoading = _this$props4$removeDr === void 0 ? false : _this$props4$removeDr;
      var _this$state$addDrawer = this.state.addDrawerVisible,
          addDrawerVisible = _this$state$addDrawer === void 0 ? false : _this$state$addDrawer;
      var historyRowSelection = {
        selectedRowKeys: systemNoticeHistorySelectedRowKeys,
        onChange: this.handleHistoryTableRowChange
      };
      var historyColumns = this.getHistoryColumns();
      var receiveColumns = this.getReceiveColumns();
      return /*#__PURE__*/React.createElement(Drawer, {
        title: intl.get('cmsg.notice.view.message.title.publish', {
          type: record.receiverTypeCode === 'NOTIFY' ? intl.get('cmsg.notice.model.notice.receiverTypeCode.notify').d('系统通知') : intl.get('cmsg.notice.model.notice.receiverTypeCode.ANNOUNCE').d('系统公告')
        }).d("\u53D1\u5E03".concat(record.receiverTypeCode === 'NOTIFY' ? intl.get('cmsg.notice.model.notice.receiverTypeCode.notify').d('系统通知') : intl.get('cmsg.notice.model.notice.receiverTypeCode.ANNOUNCE').d('系统公告'))),
        visible: visible,
        onClose: this.handleClose,
        width: 1000
      }, /*#__PURE__*/React.createElement(Spin, {
        spinning: querySystemHistoryLoading || publishSystemNoticeLoading || queryReceiverLoading
      }, /*#__PURE__*/React.createElement(Row, EDIT_FORM_ROW_LAYOUT, /*#__PURE__*/React.createElement(Col, FORM_COL_2_LAYOUT, /*#__PURE__*/React.createElement(Card, {
        bordered: false,
        title: /*#__PURE__*/React.createElement("div", {
          style: {
            display: 'flex',
            justifyContent: 'space-between'
          }
        }, /*#__PURE__*/React.createElement("span", null, intl.get('cmsg.notice.view.title.publishedHistory').d('发布记录'))),
        className: DETAIL_CARD_TABLE_THIRD_CLASSNAME
      }, /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: "publishedId",
        pagination: systemNoticeHistoryPagination,
        dataSource: systemNoticeHistoryDataSource,
        rowSelection: historyRowSelection,
        onChange: this.handleHistoryTableChange,
        columns: historyColumns,
        style: {
          marginTop: '4px'
        }
      }))), /*#__PURE__*/React.createElement(Col, FORM_COL_2_LAYOUT, /*#__PURE__*/React.createElement(Card, {
        bordered: false,
        title: /*#__PURE__*/React.createElement("div", {
          style: {
            display: 'flex',
            justifyContent: 'space-between'
          }
        }, /*#__PURE__*/React.createElement("span", null, intl.get('cmsg.notice.view.title.receiveHistory').d('接收记录')), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Button, {
          type: "primary",
          onClick: this.handlePublishBtnClick,
          disabled: removeDraftReceiveLoading || systemNoticeHistorySelectedRows.length === 0,
          loading: publishSystemNoticeLoading,
          style: {
            marginRight: '8px'
          }
        }, intl.get('cpaas.common.button.republish').d('重新发布')), /*#__PURE__*/React.createElement(Button, {
          onClick: this.handleAddBtnClick,
          disabled: removeDraftReceiveLoading
        }, intl.get('cpaas.common.button.add').d('新增')))),
        className: DETAIL_CARD_TABLE_THIRD_CLASSNAME
      }, /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: historyRowKeyFunc,
        pagination: systemNoticeReceivePagination,
        dataSource: systemNoticeReceiveDataSource,
        onChange: this.handleReceiveTableChange,
        columns: receiveColumns
      }))))), addDrawerVisible && /*#__PURE__*/React.createElement(SystemNoticePublishAddDrawer, {
        receiverRecordType: receiverRecordType,
        visible: addDrawerVisible,
        onClose: this.handleAddDrawerClose,
        onAddReceiver: this.handleAddReceiver,
        record: record,
        organizationId: organizationId,
        isTenantRole: isTenantRole,
        createReceiverLoading: createReceiverLoading
      }));
    }
  }]);

  return SystemNoticePublishDrawer;
}(Component), (_applyDecoratedDescriptor(_class.prototype, "handleClose", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleClose"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleHistoryTableChange", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleHistoryTableChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleHistoryTableRowChange", [_dec3, _dec4], Object.getOwnPropertyDescriptor(_class.prototype, "handleHistoryTableRowChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleReceiveTableChange", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "handleReceiveTableChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRemoveDraftReceive", [_dec6], Object.getOwnPropertyDescriptor(_class.prototype, "handleRemoveDraftReceive"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleAddBtnClick", [_dec7], Object.getOwnPropertyDescriptor(_class.prototype, "handleAddBtnClick"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handlePublishBtnClick", [_dec8], Object.getOwnPropertyDescriptor(_class.prototype, "handlePublishBtnClick"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleAddDrawerClose", [_dec9], Object.getOwnPropertyDescriptor(_class.prototype, "handleAddDrawerClose"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleAddReceiver", [_dec10], Object.getOwnPropertyDescriptor(_class.prototype, "handleAddReceiver"), _class.prototype)), _class));
export { SystemNoticePublishDrawer as default };